package com.gorkr.labuladong.data_struct.array;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.LinkedList;

/**
 * @author gorkr
 * @date 2022/07/25 16:10
 **/
public class E350IntersectionOfTwoArraysII {

    public int[] intersect(int[] nums1, int[] nums2) {
        LinkedList<Integer> res = new LinkedList<>();

        Arrays.sort(nums1);
        Arrays.sort(nums2);

        int end1 = nums1.length - 1;
        int end2 = nums2.length - 1;
        int i = 0, j = 0;
        while(i<=end1 && j<= end2){
            if(nums1[i]==nums2[j]){
                res.add(nums1[i]);
                i++;
                j++;
            } else if (nums1[i] < nums2[j]) {
                i++;
            }else {
                j++;
            }
        }
        return res.stream().mapToInt(m->m).toArray();
    }

    @Test
    public void test(){
        int[] nums1 = {1,2,2,1}, nums2 = {2,2};
        System.out.println(Arrays.stream(intersect(nums1, nums2)).toArray());
    }

}
    